package com.shanghaionstar.carmanage;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.utils.RxBus;
import com.shanghaionstar.R;
import com.shanghaionstar.carmanage.pay.alipay.AliPayUtil;
import com.shanghaionstar.carmanage.pay.alipay.AliWebPayRepEntity;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.weight.TopLayout;

/**
 * 支付宝web支付
 *
* @ClassName: AliWebPayActivity
* @Description: AliWebPayActivity
* @author Yonglang Hong
 */
@ContentView(R.layout.car_manage_activity_aliwebpay)
public class AliWebPayActivity extends SwipeActivity {

	public static final String ALIWEB_PAY = "aliWebPay";

	private AliWebPayRepEntity webPayRepEntity;

	@FindViewById
	private TopLayout top_layout;
	@FindViewById
	private WebView     webview;
	@FindViewById
	private ProgressBar pb_loading;

	@Override
	public BasePresenter getPresenter() {
		return null;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	private void init(){
		setUpWebView();
		getParams();
		setTitle();
		loadWebView();
	}

	private void getParams(){
		Bundle bundle = getIntent().getExtras();
		if (bundle != null){
			webPayRepEntity = (AliWebPayRepEntity)bundle.getSerializable(ALIWEB_PAY);
		}
	}

	private void loadWebView(){
		webview.loadUrl(webPayRepEntity.getWapUrl());
	}

	private void setTitle(){
		if (webPayRepEntity != null)
		    top_layout.setTopTitle(webPayRepEntity.getSubject());
	}

	private void setUpWebView(){
		webview.getSettings().setJavaScriptEnabled(true);
		webview.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onCloseWindow(WebView window) {
				super.onCloseWindow(window);
				finish();
			}
		});
		webview.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				pb_loading.setVisibility(View.GONE);
			}
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				pb_loading.setVisibility(View.VISIBLE);
			}
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
			@Override
			public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
				if (url.contains(webPayRepEntity.getCallBackUrl())) {
					goBack();
				}
				return super.shouldInterceptRequest(view, url);
			}
		});
	}


	private void goBack() {
		RxBus.getDefault().post(AliPayUtil.ALIWEB_PAY_SUCCESS,webPayRepEntity.getOutTradeNo());
		finish();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		top_layout = null;
	}
}
